package cn.tecnova.Synchronous

import java.sql.Timestamp

import cn.tecnova.bean.AppcUserSubject
import cn.tecnova.utils.ConfigHandler
import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext

/**
  * description:
  * Rabcheng
  * Date:2019/4/24 9:39
  **/
object UserSubject2Es {

  Logger.getLogger("org").setLevel(Level.ERROR)
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)
      //      .setMaster("local[*]")
      .set("es.index.auto.create", "true")
      .set("es.nodes", ConfigHandler.esNodes)
      .set("es.port", ConfigHandler.esPort)
      .set("es.nodes.wan.only", "true")

    val sc = new SparkContext(conf)
    val sqLContext = new SQLContext(sc)

    import sqLContext.implicits._
    val userSubjectDF = sqLContext.read.jdbc(ConfigHandler.url,"user_subject",ConfigHandler.props)

    //导入写es相关的包
    import cn.tecnova.bean.IntString._
    import org.elasticsearch.spark.sql._

    val res2 = userSubjectDF.map(row=>{
      AppcUserSubject(
        row.getAs[String]("id"),
        row.getAs[String]("user_id"),
        row.getAs[Int]("subject_type").toStringPlusInt,
        row.getAs[String]("full_name"),
        row.getAs[String]("short_name"),
        row.getAs[String]("modify_name"),
        row.getAs[String]("rel_subject_id"),
        row.getAs[String]("rel_subject_name"),
        row.getAs[String]("response_person"),
        row.getAs[String]("industry"),
        row.getAs[String]("egistered_address"),
        row.getAs[String]("industry_fund"),
        row.getAs[Int]("focus_flag").toStringPlusInt,
        row.getAs[String]("focus_reason"),
        row.getAs[Int]("weight").toStringPlusInt,
        row.getAs[String]("description"),
        row.getAs[String]("microblog"),
        row.getAs[String]("wechat"),
        row.getAs[String]("qq"),
        row.getAs[String]("main_source"),
        row.getAs[Timestamp]("created_time").toString,
        row.getAs[String]("update_user_id"),
        row.getAs[Timestamp]("updated_time").toString)
    })

    res2.saveToEs("appc_user_subject" + "/appc_user_subject")

    sc.stop()

  }

}
